\chapter{FreeBSD Jails}
\label{jails}
L3DGEWorld Asterisk Management System (LAMS) provides network administrators, VoIP and Internet service providers to monitoring a cluster of VoIP servers. As LAMS is a prototype and due to hardware availability and time restraints, VoIP clusters can be created by using FreeBSD jails.

FreeBSD jails are sub systems within a host FreeBSD system that act as real systems. Jails are used to imprison services such as web and name servers. If the server is attacked or corrupted, the whole system is not compromised, only the jail. Jails are also useful for small scale system testing. 
 
This report outlines the steps taken to implement more than one identical FreeBSD jails for networking and testing purposes. Each jail is configured to run Asterisk 1.4 and act as an individual IP PBX. 

\section{Scenario of testing}
For testing/prototyping purposes we want to simulate a scaled down LAMS system. These consist of three VoIP servers, at least four to ten VoIP clients for each VoIP server and one LAMS monitoring server. Due to time and hardware availability, we are forced to simulate a working scaled down version of LAMS. 

The following diagram describes the logical and actually physical set up of our LAMS system. 
\begin{figure}[h] 
%\hspace{6cm}
%\vspace{5cm}
\includegraphics[scale=0.7]{diagrams/jail_setup.pdf}
\end{figure} 


\section{Hardware Specification}

According to Asterisk PBX documentation a 3 GHz system with 1 GB of RAM can support up to 25 channels [insert reference]. 
Have three Asterisk jail systems with 3 --5 phones requiring a channel each leaves us with left over CPU power for others applications we will be running. 

Our host jail system has the following system specifications:

CPU: Intel Celeron 2.8GHz
RAM: 1024 MB
Hard Drive: 60 GB

\section{FreeBSD Installation \& Jail Building}

A standard FreeBSD 7.0 installation with the source files and ports tree was made on our host jail system.  

\section{Creation of first jail}

Majority of the steps followed correspond to the FreeBSD handbook documentation [insert reference], but with added steps here and there. 
\begin{verbatim}
#mkdir - p /usr/home/jail/asterisk1
#cd /usr/src
#make world DESTDIR=/usr/home/jail/asterisk1
#cd etc/
#make distribution DESTDIR=/usr/home/jail/askterisk1
#mount - t devfs devfs /usr/home/jail/asterisk1/dev
\end{verbatim}

\section{Host network settings}
Each jail can be assigned another address. The host system needs to alias the address to the network card. 
\begin{verbatim}
#ifconfig fxp0 192.168.18.36 netmask 255.255.255.0 alias
\end{verbatim}
To start jail
\begin{verbatim}
#jail /usr/home/jail/asterisk1 pbx1.lams.home 192.168.18.36 /bin/tcsh
\end{verbatim}

A shell prompt and you should now be inside your jail
\begin{verbatim}
 pbx1#
\end{verbatim}
 
Create an empty /etc/fstab file so at startup there is no complaining.
 
Edit \begin{verbatim}/etc/rc.conf\end{verbatim} with the following
\begin{itemize}
\item IP address
\item Hostname
\item rpcbind\_enable=``NO''
\item default route
\item sshd\_enable=``YES''
\end{itemize}
Edit /etc/resolv.conf for DNS resolving
Add a primary user
Create a root password

Once created, run the following to load /etc/rc.conf
\begin{verbatim}
 pbx1# /bin/sh /etc/rc
\end{verbatim}
sshd and other services you have enabled in /etc/rc.conf should be running. 

%

\section{Jail Management}
From the host system, jails can be managed with the following:
\begin{verbatim}
// can be used to start a jail
#jail [path to jail] [hostname] [ip address] [shell to start] 

#jls // lists all the jails running on the host system

jexec // can be used to enter an existing jail from the host system. 
\end{verbatim}


\section{Building Asterisk Jail 1}

From the host system, the jail should have a basic FreeBSD file system. To be able to build asterisk, the src and port tree should be added. 

\begin{verbatim}
#mkdir -p /usr/home/jail/asterisk1/usr/ports
#mount_nullfs /usr/ports /usr/home/jail/asterisk1/usr/ports

#mount_nullfs /usr/src /usr/home/jail/asterisk1/usr/src
\end{verbatim}

Once inside the jail, the ports tree is read only, thus the WRKDIRPREFIX and DISTDIR need to be specified as an environmental variable.
 \begin{verbatim}
 pbx1#mkdir /usr/portswork
 pbx1#set env WRKDIRPREFIX=/usr/portswork
 pbx1#set env DISTDIR=/usr/portsworks/usr/ports/distfiles
\end{verbatim}
Now from within the jail, ports building will work. 


\section{Cloning Asterisk Jail 1}

Our first jail is up and running with all the modifications we want. We want another two identical. To clone the first jail all that is required is to copy the jail root directory to another location making sure to preserve. 

\begin{verbatim}
cp - pR asterisk1 asterisk2
cp - pR asterisk1 asterisk3
\end{verbatim}

Each jail needs the following tasks

\begin{itemize}
\item edit the [asterisk]/etc/rc.conf - for the hostname and ip address
\item touch the etc/fstab
\item any sripts or daemons specific to hosts.
\item assign the IP address alias on the host system.
\end{itemize}


